package com.xintoucloud.gsv.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintoucloud.gsv.entity.RolePermission;
import com.xintoucloud.gsv.mapper.RolePermissionMapper;
import com.xintoucloud.gsv.model.MenuRoleVo;
import com.xintoucloud.gsv.model.Status;
import com.xintoucloud.gsv.model.TreeData;
import com.xintoucloud.gsv.model.TreeNode;
import com.xintoucloud.gsv.service.RolePermissionService;

@Service
public class RolePermissionServiceImpl implements RolePermissionService {

	@Autowired
	RolePermissionMapper rolePermissionMapper;
	
	@Override
	public List<RolePermission> list() {
		List<RolePermission> rolePermissions = rolePermissionMapper.list();
		return rolePermissions;
	}
	
	@Override
	public TreeData getTreeData(int rid) {
		TreeData treeData = new TreeData();
		Status status=new Status();
		int parentId = 0;
		List<TreeNode> treeNodes = listTreeNode(rid, parentId);
		if(treeNodes!=null) {
			status.setCode(200);
			status.setMessage("加载成功");
		}
			treeData.setData(treeNodes);
			treeData.setStatus(status);
		return treeData;
	}

	@Override
	public List<TreeNode> listTreeNode(int rid,int parentId) {
		List<TreeNode> treeNodes = new ArrayList<>();
		List<MenuRoleVo> menuRoleVos = rolePermissionMapper.menuZeroParentIdListByRid(rid,parentId);
		//System.out.println("一级菜单数menuRoleVos:"+menuRoleVos.size());
		for (MenuRoleVo menuRoleVo : menuRoleVos) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(menuRoleVo.getId());
			treeNode.setTitle(menuRoleVo.getMenuName());
			treeNode.setParentId(menuRoleVo.getMenuParentId());
//			List<CheckArr> checkArrs = new ArrayList<>();
//			CheckArr checkArr = new CheckArr();
//			checkArr.setIsChecked("0");
//			checkArr.setType("0");
//			checkArrs.add(checkArr);
//			treeNode.setCheckArr(checkArrs);
			//parentId += 1;
			List<TreeNode> childs = listTreeNode(rid,menuRoleVo.getMenuId());
			treeNode.setChildren(childs);
			if(childs.isEmpty()) {
				treeNode.setisLast(true);
			}else {
				treeNode.setisLast(false);
			}
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}
}
